<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商家名称" prop="mname">
        <el-input
          v-model="queryParams.mname"
          placeholder="请输入商家名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mphone">
        <el-input
          v-model="queryParams.mphone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证" prop="idNumber">
        <el-input
          v-model="queryParams.idNumber"
          placeholder="请输入身份证"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核是否通过1通过2未通过" prop="audit">
        <el-input
          v-model="queryParams.audit"
          placeholder="请输入审核是否通过1通过2未通过"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付账号" prop="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入支付账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="菜品id" prop="cuisineId">
        <el-input
          v-model="queryParams.cuisineId"
          placeholder="请输入菜品id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同图片" prop="contract">
        <el-input
          v-model="queryParams.contract"
          placeholder="请输入合同图片"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户id" prop="uid">
        <el-input
          v-model="queryParams.uid"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付金额" prop="monetary">
        <el-input
          v-model="queryParams.monetary"
          placeholder="请输入支付金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="银行卡" prop="bankCard">
        <el-input
          v-model="queryParams.bankCard"
          placeholder="请输入银行卡"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:merchant:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:merchant:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:merchant:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:merchant:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="merchantList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="mid" />
      <el-table-column label="商家名称" align="center" prop="mname" />
      <el-table-column label="手机号" align="center" prop="mphone" />
      <el-table-column label="身份证" align="center" prop="idNumber" />
      <el-table-column label="审核是否通过1通过2未通过" align="center" prop="audit" />
      <el-table-column label="支付账号" align="center" prop="account" />
      <el-table-column label="菜品id" align="center" prop="cuisineId" />
      <el-table-column label="合同图片" align="center" prop="contract" />
      <el-table-column label="用户id" align="center" prop="uid" />
      <el-table-column label="支付金额" align="center" prop="monetary" />
      <el-table-column label="银行卡" align="center" prop="bankCard" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:merchant:edit']"
          >支付</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:merchant:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:merchant:remove']"
          >删除</el-button>

          <router-link :to="{path: '/demo',query:{index:scope.row}}">
            <button class="btn btn-default">扫描桌上二维码</button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商家对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商家名称" prop="mname">
          <el-input v-model="form.mname" placeholder="请输入商家名称" />
        </el-form-item>
        <el-form-item label="手机号" prop="mphone">
          <el-input v-model="form.mphone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="身份证" prop="idNumber">
          <el-input v-model="form.idNumber"  placeholder="请输入身份证" />
          <el-button @click="idcheck()">检查</el-button>
        </el-form-item>
        <el-form-item label="审核是否通过1通过2未通过" prop="audit">
          <el-input v-model="form.audit" placeholder="请输入审核是否通过1通过2未通过" />
        </el-form-item>
        <el-form-item label="支付账号" prop="account">
          <el-input v-model="form.account" placeholder="请输入支付账号" />
        </el-form-item>
<!--        <el-form-item label="菜品id" prop="cuisineId">-->
<!--          <el-input v-model="form.cuisineId" placeholder="请输入菜品id" />-->
<!--        </el-form-item>-->
        <el-form-item label="营业执照" prop="contract">
          <el-upload
            class="avatar-uploader"
            action="http://localhost/dev-api/merchant/merchant/oss"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="form.contract" :src="form.contract" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
          <el-button @click="businessCheck1()">检测</el-button>
        </el-form-item>
        <el-form-item label="用户id" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="支付金额" prop="monetary">
          <el-input v-model="form.monetary" placeholder="请输入支付金额" />
        </el-form-item>
        <el-form-item label="银行卡" prop="bankCard">
          <el-input v-model="form.bankCard" placeholder="请输入银行卡" />
          <el-button @click="bankcheck()">检查</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {
  listMerchant,
  getMerchant,
  delMerchant,
  addMerchant,
  updateMerchant,
  idcheck,
  businessCheck
} from "@/api/merchant/merchant";
import { bankcheck } from '../../../api/merchant/merchant'

export default {
  name: "Merchant",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商家表格数据
      merchantList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数

      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mname: null,
        mphone: null,
        idNumber: null,
        audit: null,
        account: null,
        cuisineId: null,
        contract: null,
        uid: null,
        monetary: null,
        bankCard: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.contract=this.imageUrl
    },
    bankcheck(){
      let bankCard=this.form.bankCard
      bankcheck(bankCard).then((res)=>{
        if(response=="200"){
          alert('成功')
        }else{
          alert('失败')
        }
      })
    },
    businessCheck1(){
      businessCheck().then((res)=>{
        alert(res)
      })
    },
    idcheck(){
      let name=this.form.mname
      let id=this.form.idNumber
      idcheck(id,name).then(response=>{
        if(response=="200"){
          alert('成功')
        }else{
          alert('失败')
        }
      })
    },
    /** 查询商家列表 */
    getList() {
      this.loading = true;
      listMerchant(this.queryParams).then(response => {
        this.merchantList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        mid: null,
        mname: null,
        mphone: null,
        idNumber: null,
        audit: null,
        account: null,
        cuisineId: null,
        contract: null,
        uid: null,
        monetary: null,
        bankCard: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.mid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商家";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const mid = row.mid || this.ids
      getMerchant(mid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商家";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.mid != null) {
            updateMerchant(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMerchant(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const mids = row.mid || this.ids;
      this.$modal.confirm('是否确认删除商家编号为"' + mids + '"的数据项？').then(function() {
        return delMerchant(mids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    //扫描二维码
    scan(row){
      const mid = row.mid || this.ids
      this.$router.push({path: 'demo/qrcodeStream',query:{ index:this.row}});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/merchant/export', {
        ...this.queryParams
      }, `merchant_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
